package com.ruoyi.yn.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yn.vo.YnUserOrderVo;
import com.ruoyi.yn.service.IYnUserOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户订单Controller
 *
 * @author Ming
 * @date 2023-04-25
 */
@Api(tags = "用户订单Controller")
@RestController
@RequestMapping("/yn/userOrder")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class YnUserOrderController extends BaseController {

    private final IYnUserOrderService ynUserOrderService;

    @ApiOperation("查询用户订单列表")
    @PreAuthorize("@ss.hasPermi('yn:userOrder:list')")
    @GetMapping("/list")
    public TableDataInfo<YnUserOrderVo> list(YnUserOrderVo entity) {
        return ynUserOrderService.queryList(entity);
    }

    @ApiOperation("查询用户订单所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(YnUserOrderVo entity) {
        return AjaxResult.success("查询成功", ynUserOrderService.queryAll(entity));
    }

    @ApiOperation("导出用户订单列表")
    @PreAuthorize("@ss.hasPermi('yn:userOrder:export')")
    @Log(title = "用户订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YnUserOrderVo entity) {
        List<YnUserOrderVo> list = ynUserOrderService.queryAll(entity);
        ExcelUtil<YnUserOrderVo> util = new ExcelUtil<>(YnUserOrderVo.class);
        util.exportExcel(response, list, "用户订单数据");
    }

    @ApiOperation("获取用户订单详细信息")
    @PreAuthorize("@ss.hasPermi('yn:userOrder:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success("查询成功", ynUserOrderService.queryById(id));
    }

    @ApiOperation("新增用户订单")
    @PreAuthorize("@ss.hasPermi('yn:userOrder:add')")
    @Log(title = "用户订单", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody YnUserOrderVo entity) {
        return toAjax(ynUserOrderService.save(entity));
    }

    @ApiOperation("修改用户订单")
    @PreAuthorize("@ss.hasPermi('yn:userOrder:edit')")
    @Log(title = "用户订单", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody YnUserOrderVo entity) {
        return toAjax(ynUserOrderService.updateById(entity));
    }

    @ApiOperation("删除用户订单")
    @PreAuthorize("@ss.hasPermi('yn:userOrder:remove')")
    @Log(title = "用户订单", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(ynUserOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
